package com.villaday.idf.base;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener;
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager;

import com.villaday.idf.base.broadcast.RxManager;
import com.villaday.idf.model.MsgEntity;

/**
 * 所有Fragment的基类
 */
public abstract class BaseFragment extends Fragment {

    private Unbinder unbinder;

    protected OnMsgListener msgListener;
    protected OnCartUpListener cartListener;
    //RxJava事件总线
    public RxManager mRxManager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(getViewResId(), container, false);
        unbinder = ButterKnife.bind(this, v);
        mRxManager=new RxManager();

        return v;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        init(view);
        getData(getArguments());
        loadData();
        super.onViewCreated(view, savedInstanceState);
    }

    protected abstract int getViewResId();

    protected void init(View v){}

    protected void getData(Bundle arguments){}

    protected  void loadData(){}

    public void setOnMsgListener(OnMsgListener msgListener){
        this.msgListener = msgListener;
    }

    public interface OnMsgListener{
        void onDeliverMsg(MsgEntity msg);
    }

    public void setOnCartUpListener(OnCartUpListener cartListener) {
        this.cartListener = cartListener;
    }

    public interface OnCartUpListener {
        void onCartAnim(View v, String url);
        void onCartUp(int count);
    }

    public void upUI(String info){}

    @Override
    public void onDestroy() {
        if (mRxManager!=null){
            mRxManager.clear();
        }
        unbinder.unbind();
        super.onDestroy();
    }

    public StatusLayoutManager setupDefaultStatusLayoutManager(View rootView, OnStatusChildClickListener onStatusChildClickListener) {
        StatusLayoutManager statusLayoutManager = new StatusLayoutManager.Builder(rootView)

                // 设置默认布局属性
//                .setDefaultEmptyText("空白了，哈哈哈哈")
//                .setDefaultEmptyImg(R.mipmap.ic_launcher)
//                .setDefaultEmptyClickViewText("retry")
//                .setDefaultEmptyClickViewTextColor(getResources().getColor(R.color.colorAccent))
//                .setDefaultEmptyClickViewVisible(false)
//
//                .setDefaultErrorText(R.string.app_name)
//                .setDefaultErrorImg(R.mipmap.ic_launcher)
//                .setDefaultErrorClickViewText("重试一波")
//                .setDefaultErrorClickViewTextColor(getResources().getColor(R.color.colorPrimaryDark))
//                .setDefaultErrorClickViewVisible(true)
//
//                .setDefaultLayoutsBackgroundColor(Color.WHITE)

                // 自定义布局
//                .setLoadingLayout(inflate(R.layout.layout_loading))
//                .setEmptyLayout(inflate(R.layout.layout_empty))
//                .setErrorLayout(inflate(R.layout.layout_error))
//
//                .setLoadingLayout(R.layout.layout_loading)
//                .setEmptyLayout(R.layout.layout_empty)
//                .setErrorLayout(R.layout.layout_error)
//
//                .setEmptyClickViewID(R.id.tv_empty)
//                .setErrorClickViewID(R.id.tv_error)

                // 设置重试事件监听器
                .setOnStatusChildClickListener(onStatusChildClickListener)
                .build();

        return statusLayoutManager;
    }

    //页面跳转
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(getActivity(), clz));
//        overridePendingTransition(R.anim.push_out_right, R.anim.push_out_left);
    }

    /**
     * 带数据的页面跳转
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clz);
        intent.putExtras(bundle);
        startActivity(intent);
//        overridePendingTransition(R.anim.push_out_right, R.anim.push_out_left);
    }

    /**
     * 带返回的Activity的方式
     */
    public void startActivityResult(Class<?> clz, int code) {
        startActivityForResult(new Intent(getActivity(), clz), code);
    }

    /**
     * 带返回的Activity的方式
     *
     * @param clz
     * @param code
     * @param bundle
     */
    public void startActivityResult(Class<?> clz, int code, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clz);
        intent.putExtras(bundle);
        startActivityForResult(intent, code);
    }
}
